from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QFrame, QMenu
from PySide6.QtCore import Qt
from PySide6.QtGui import QCursor
from common import config

class BasePanel(QWidget):
    def __init__(self, title):
        super().__init__()
        self._setup_ui(title)

    def _setup_ui(self, title):
        self.setObjectName("basePanel")
        self.main_layout = QVBoxLayout(self)
        # 顶部标题栏
        title_bar = QWidget()
        title_bar.setObjectName("titleBar")
        title_layout = QHBoxLayout(title_bar)
        title_label = QLabel(title)
        title_label.setObjectName("titleLabel")
        title_layout.addWidget(title_label)
        title_layout.addStretch()
        self.main_layout.addWidget(title_bar)

    def setup_context_menu(self):
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.show_context_menu)

    def show_context_menu(self, pos):
        menu = QMenu(self)
        menu.setObjectName("baseMenu")
        refresh_menu = menu.addAction("刷新")
        refresh_menu.triggered.connect(self.load_data)

        self.add_extra_context_menus(menu, pos)

        menu.addSeparator()
        lock_menu = menu.addAction("锁屏")
        lock_menu.triggered.connect(self.get_top_window().lock_screen)
        loggout_menu = menu.addAction("退出")
        loggout_menu.triggered.connect(self.get_top_window().loggout)

        menu.exec(QCursor.pos())

    def get_top_window(self):
        return self.parent().parent().parent().parent()

    def add_extra_context_menus(self, menu, pos):
        pass


    def load_data(self):
        pass

class CardPanel(QFrame):
    def __init__(self, parent, title, value, subtitle, subvalue):
        super().__init__(parent)
        self.setObjectName("dashboardCard")
        layout = QVBoxLayout(self)
        
        # 主标题
        title_label = QLabel(title)
        title_label.setObjectName("mainTitle")
        layout.addWidget(title_label)
        
        # 主数值
        self.value_label = QLabel(value)
        self.value_label.setObjectName("mainValue")
        layout.addWidget(self.value_label)
        
        # 分隔线
        line = QLabel()
        line.setObjectName("separator")
        layout.addWidget(line)
        
        # 子标题和子数值
        sub_widget = QWidget()
        sub_layout = QHBoxLayout(sub_widget)
        sub_layout.setContentsMargins(0, 0, 0, 0)
        
        subtitle_label = QLabel(subtitle)
        subtitle_label.setObjectName("subTitle")
        self.subvalue_label = QLabel(subvalue)
        self.subvalue_label.setObjectName("subValue")
        
        sub_layout.addWidget(subtitle_label)
        sub_layout.addWidget(self.subvalue_label)
        
        layout.addWidget(sub_widget)

    def update_value(self, value, subvalue):
        self.value_label.setText(str(value))
        self.subvalue_label.setText(str(subvalue))